package user

import (
	"encoding/json"
	"gitee.com/go-web-2/business/config"
	"gitee.com/go-web-2/business/errors/session_errors"
	"gitee.com/go-web-2/web_framework/context"
	errors2 "gitee.com/go-web-2/web_framework/errors"
	"log"
)

type SessionUser struct {
	Name     string `json:"name"`
	Address  string `json:"address"`
	Username string `json:"username"`
	Phone    string `json:"phone"`
	Email    string `json:"email"`
	Id       string `json:"id"`
	Avatar   string `json:"avatar"`
}

func GetUserInfo(c *context.Context) {
	sess, err := config.MG.GetSession(c)
	if err != nil {
		log.Println(err)
		c.RespSystemErr(session_errors.SessionNotFound.Error())
		return
	}
	var val interface{}
	val, err = sess.Get(c, sess.ID())
	if err != nil {
		log.Println(err)
		c.RespSystemErr(session_errors.SessionNotFound.Error())
		return
	}
	u := &SessionUser{}
	err = json.Unmarshal([]byte(val.(string)), u)
	if err != nil {
		c.RespSystemErr(errors2.ServerError.Error())
		return
	}
	c.RespOk(u)
}
